#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
   @Author: 叶建伟
   @Email: yejw46507@hundsun.com
   @Time: 2023/7/5 13:58
   @File: threadInsertBTA.py
"""
import threading
import queue
from queue import Empty
from random import choice

from DataTest.databases import MysqlUtil, DaMengUtil
import datetime

# 定义全局变量num_threads线程数量，num_inserts插入数据总量，batch_size单次插入数量

num_threads = 1



processes = []

# 定义队列用于存储拼接好的SQL语句
sql_queue = queue.Queue()

# 定义锁，用于保证线程安全
lock = threading.Lock()

BLOCK=True


# 定义线程执行的函数：SQL拼接
def sql_assemble(thread_id):
        temp = 0
        for j in range(50000):
            sql = 'insert into hsfund.ods_third_nj_flag(d_date,table_name,is_etl,is_insert_etl,is_update_etl,is_dealdata) values'
            for i in range(20):
                d_tate = '20191128'
                is_etl = choice(['0','1','2','3','4','5','6','7','8','9'])
                is_insert_etl =  choice(['0','1','2','3','4','5','6','7','8','9'])
                is_update_etl = choice(['0','1','2','3','4','5','6','7','8','9'])
                is_dealdata = choice(['0','1','2','3','4','5','6','7','8','9'])
                table_name = 'zhanhao_'+str(temp)
                if i == 19:
                    sql = sql+"('"+d_tate+"','"+table_name+"','"+is_etl+"','"+is_insert_etl+"','"+is_update_etl+"','"+is_dealdata+"')"+";"
                    sql_queue.put(sql)
                    sql = 'insert into hsfund.ods_third_nj_flag(d_date,table_name,is_etl,is_insert_etl,is_update_etl,is_dealdata) values'
                else:
                    sql = sql+"('"+d_tate+"','"+table_name+"','"+is_etl+"','"+is_insert_etl+"','"+is_update_etl+"','"+is_dealdata+"')"+","
                temp = temp+1

# 定义线程执行的函数：SQL插入
def sql_insert(db):
    # while not sql_queue.empty():
    global sql
    while True:
        lock.acquire(blocking=True,timeout=-1)
        try:
            sql = sql_queue.get(block=BLOCK)
            # print(f'插入sql:{sql}\n')
            # 执行SQL插入操作
        except Empty as e:
            break
        finally:
            lock.release()
        if sql:
            print(sql)
            db.execsql(sql)
        else:
            break





if __name__ == '__main__':
    print(datetime.datetime.now())
    # 创建并启动SQL拼接线程
    assemble_threads = []
    for i in range(num_threads):
        thread = threading.Thread(target=sql_assemble, args=(i,))
        thread.start()
        assemble_threads.append(thread)


   # 创建并启动SQL插入线程
    insert_threads = []
    db = MysqlUtil('root', 'fxhg@1234', '10.20.181.116', port=33061)

    for i in range(num_threads):
        thread = threading.Thread(target=sql_insert,args=(db,))
        thread.start()
        insert_threads.append(thread)

    #等待所有SQL拼接线程执行完毕
    for thread in assemble_threads:
        thread.join()
    else:
        BLOCK=False
        sql_queue.put(False)

    # # 等待所有SQL插入线程执行完毕
    # for thread in insert_threads:
    #     thread.join()
    print(datetime.datetime.now())
